package com.yunxie.novelservice.domain.service.impl;

import com.alibaba.fastjson2.JSON;
import com.yunxie.novelservice.common.entity.PageResult;
import com.yunxie.novelservice.domain.convert.NovelsBoConverter;
import com.yunxie.novelservice.domain.entity.NovelsBo;
import com.yunxie.novelservice.domain.service.NovelsDomainService;
import com.yunxie.novelservice.infra.basic.entity.Novels;
import com.yunxie.novelservice.infra.basic.service.NovelsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class NovelsDomainServiceImpl implements NovelsDomainService {

    @Resource
    NovelsService novelsService;

    @Override
    public NovelsBo queryNovels(Long novelsId) {
        Novels novels = novelsService.queryById(novelsId);
        NovelsBo novelsBo = NovelsBoConverter.INSTANCE.convertEntityToBo(novels);
        return novelsBo;
    }

    @Override
    public void addNovels(NovelsBo novelsBo) {
        if (log.isInfoEnabled()) {
            log.info("NovelsDomainServiceImpl.addNovels.bo:{}", JSON.toJSON(novelsBo));
        }
        Novels novels = NovelsBoConverter.INSTANCE.convertBoToEntity(novelsBo);
        //初始为未完结以及未删除
        novels.setIsCompleted(0);
        novels.setIsDeleted(0);
        novelsService.insert(novels);
    }


    @Override
    public void updateNovels(NovelsBo novelsBo) {
        if (log.isInfoEnabled()) {
            log.info("NovelsDomainServiceImpl.updateNovels.bo:{}", JSON.toJSON(novelsBo));
        }
        Novels novels = NovelsBoConverter.INSTANCE.convertBoToEntity(novelsBo);
        //初始为未完结以及未删除

        novelsService.update(novels);
    }


/**
 * 查询小说列表（分页）
 *
 * @param novelsBo 包含分页参数和查询条件的小说业务对象
 * @return 分页结果对象，包含小说列表和分页信息
 */
     @Override
    public PageResult<NovelsBo> queryNovelsList(NovelsBo novelsBo) {
        // 创建分页结果对象
        PageResult<NovelsBo> pageResult = new PageResult<>();

        // 设置分页参数
        pageResult.setPageNo(novelsBo.getPageNo());
        pageResult.setPageSize(novelsBo.getPageSize());

        // 计算分页起始位置
        int start = (novelsBo.getPageNo() - 1) * novelsBo.getPageSize();

        // 将 NovelsBo 转换为 Novels 实体对象
        Novels novels = NovelsBoConverter.INSTANCE.convertBoToEntity(novelsBo);

        // 查询小说列表（分页）
        List<Novels> novelsList = novelsService.queryPage(start, novelsBo.getPageSize());

        // 将查询结果转换为 NovelsBo 列表
        List<NovelsBo> novelsBoList = NovelsBoConverter.INSTANCE.convertEntityListToBoList(novelsList);

        // 设置分页结果
        pageResult.setRecords(novelsBoList);

        // 查询总记录数（假设 novelsService 提供了查询总记录数的方法）
        int total = novelsService.countNovels(novels);
        pageResult.setTotal(total);

        // 返回分页结果
        return pageResult;
    }

    @Override
    public void deleteNovels(Long novelsId) {
        //软删除
        if (log.isInfoEnabled()) {
            log.info("NovelsDomainServiceImpl.deleteNovels.bo:{}", JSON.toJSON(novelsId));
        }
        Novels novels = novelsService.queryById(novelsId);
        novels.setIsDeleted(1);
        novelsService.update(novels);

    }


}
